<?php
declare (strict_types = 1);

namespace app\controller;

/**
 * 微信小程序接口
 */

use app\common\model\Ad;
use function PHPSTORM_META\elementType;
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;
use think\Request;

class Xiaochengxu extends Common
{

    public function __construct()
    {
        parent::__construct();
        $ad = Ad::where('board_id',7)->select();
        View::assign("ad",$ad);
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {


        $cid = input("get.cid");
        if($cid){
            $where["cate_id"] = $cid;
        }
        $where["status"] =1;


        if(!isset($where)){
            $where = 1;
        }

        $list = \app\common\model\Content::with("user")
            ->where($where)
            ->order("top DESC,recommend DESC,id DESC")
            ->paginate(5,false,[
                'query'=> $this->params,
            ])->toArray();

        foreach ($list['data'] as $k => $v){
            if(isset($v["prefimg"])){
                if(!strstr($v["prefimg"],"http")){
                    $list["data"][$k]["prefimg"] = "https://micuer.com/".$v["prefimg"];
                }
            }

        }
        if($list){
            return json(["code"=>200,"msg"=>"成功","data"=>$list]);
        }else{
            return json(["code"=>400,"msg"=>"失败","data"=>$list]);
        }
    }

    public function login_xcx()
    {
        $code = input("post.code");
        $appid = 'wxf6fe9711e57615f7';
        $secret = "635a8ebd04989259b21db0e85dbb1269";
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
        $arrContextOptions = [
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
            ]
        ];
        $res = file_get_contents($url, false, stream_context_create($arrContextOptions));
        $res  = json_decode($res,true);
        $userinfo = input("post.rawData");
        $userinfo = json_decode($userinfo,true);
        $data["data"] = $res;
        $data["userinfo"] = $userinfo;
        $openid = $res["openid"];
        $user = Db::name("user")->where("openid_wx",$openid)->find();
        $update["headimgurl"] = $userinfo["avatarUrl"];
        $update["nickname"] = $userinfo["nickName"];
        $update["gender"] = $userinfo["gender"];
        $update["last_time"] = time();
        $update["last_ip"] = \think\facade\Request::ip();
        if($user){
            //用户已存在  更新用户的头像及登录信息
            Db::name("user")->where("id",$user["id"])->update($update);
        }else{
            //新用户
            $update["reg_time"] = time();
            $update["reg_ip"] = \think\facade\Request::ip();
            $update["sign_time"] = time();
            $update["openid_wx"] = $res["openid"];
            $uid = Db::name("user")->insertGetId($update);
        }
        return json($data);
    }

    //轮播
    public function swiper()
    {
        $list = Db::name("ad")->where("board_id",10)->select()->toArray();
       
        foreach ($list  as $k => $v){
            $v["img"] = handlePic($v["img"]);
            $list[$k]["img"] = $v["img"];
        }
        if($list){
            return json(["code"=>200,"msg"=>"成功","data"=>$list]);
        }else{
            return json(["code"=>400,"msg"=>"失败","data"=>$list]);
        }
    }
}
